package ywpt.service.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import ywpt.components.acegi.domain.Resource;
import ywpt.components.acegi.domain.Role;
import ywpt.core.dao.extend.HibernateEntityExtendDao;
import ywpt.core.exception.UniqueConstraintException;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataIntegrityViolationException;

/**
 * 
 * 系统角色服务实现类 2007-10-8 下午09:57:23
 * 
 * @author <a href="mailto:mrwyxster@gmail.com">Oliver Wen</a>
 * 
 */
public class RoleServiceImpl extends HibernateEntityExtendDao<Role> implements RoleService {
	public RoleServiceImpl() {
	}

	public Role getRoleById(Long id) {
		return this.get(Role.class, id);
	}

	public List<Role> getAllRoles() {
		return this.getAll(Role.class);
	}

	@SuppressWarnings("unchecked")
	public List<Role> findAllRolesByCriteria(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	public List<Role> findAllRolesByCriteria(DetachedCriteria query, int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	public void saveRole(Role role) {
		try {
			this.save(role);
		} catch (DataIntegrityViolationException e) {
			throw new UniqueConstraintException("role '" + role.getRoleName() + "' already exists!");
		}
	}

	public void updateRole(Role role) {
		this.save(role);
	}

	public void removeRole(Long id) {
		this.removeById(id);
	}

	public int[] removeRoles(Long[] ids) {
		String hql = "delete ywpt.components.acegi.domain.Role r where r.id =?";
		int results[] = new int[ids.length];
		for (int i = 0; i < ids.length; i++) {
			results[i] = super.bulkUpdate(hql, ids[i]);
		}
		return results;
	}

	public void addResources(Role role, List<Long> resources) {
		if (resources != null && resources.size() > 0) {
			role.setResources(new HashSet<Resource>(0));
			for (int i = 0, j = resources.size(); i < j; i++) {
				Resource resource = (Resource) this.get(Resource.class, resources.get(i));
				role.getResources().add(resource);
			}
			super.update(role);
		}
	}

	public int[] removeResources(Role role, Long[] ids) {
		for (int i = 0; i < ids.length; i++) {
			Resource res = super.get(Resource.class, ids[i]);
			role.removeResource(res);
		}
		super.update(role);
		int results[] = new int[ids.length];
		for (int i = 0; i < ids.length; i++) {
			results[i] = 1;
		}
		return results;
	}

	public int getRolesCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	public List getRoleTree() {
		String hql = "from ywpt.components.acegi.domain.Role as role";
		List<Role> roleList = super.find(hql);
		List jsonList = new ArrayList();
		for (int i = 0; i < roleList.size(); i++) {
			Role row = roleList.get(i);
			Map roleMap = new HashMap();
			roleMap.put("text", row.getDescription());
			roleMap.put("id", row.getId());
			/*
			 * List childrenRow =new ArrayList(); for(Iterator
			 * itr=row.getUsers().iterator();itr.hasNext();){
			 * childrenRow.add((User)itr.next()); } if (childrenRow.size() > 0) {
			 * List userJsonList=new ArrayList(); for(Iterator
			 * userItr=childrenRow.iterator();userItr.hasNext();){ User
			 * user=(User)userItr.next(); Map userMap=new HashMap();
			 * userMap.put("id",user.getUsername());
			 * userMap.put("text",user.getFullName()); userMap.put("leaf",
			 * Boolean.TRUE); userJsonList.add(userMap); }
			 * roleMap.put("children", userJsonList); } else {
			 * roleMap.put("leaf", Boolean.TRUE); //roleMap.put("url",
			 * row.getUrl()); }
			 */
			roleMap.put("leaf", Boolean.TRUE);
			roleMap.put("iconCls", "tree-node-group");
			roleMap.put("url", "../system/resource/list-" + row.getId() + ".action");
			jsonList.add(roleMap);
		}
		return jsonList;
	}

	@Override
	protected void onValid(Role entity) {
		if (!isUnique(Role.class, entity, "roleName")) {
			throw new UniqueConstraintException("角色名" + entity.getRoleName() + "重复");
		}
	}
}